﻿using System.Linq;
using ExORMDesginer.Models;
using ExtendPropertyLib;
using System.Collections.ObjectModel;

namespace ExORMDesginer.UserControls
{
    /// <summary>
    /// 参数列表业务属性类
    /// </summary>
    public class ParametersListBoxInfo : BusinessInfoBase<ParametersListBoxInfo>
    {
        /// <summary>
        /// 载入计数
        /// </summary>
         public int loadCount { set; get; }

        /// <summary>
        /// 重置载入计数器
        /// </summary>
         public void Rest()
         {
             this.loadCount = 0;
         }

        public ParametersListBoxInfo()
        {
            Parameters = new SerializableObservableCollection<ParameterInfo>();
            loadCount = 0;
        }

        static ParametersListBoxInfo()
        {
            ParameterTypeList = new ObservableCollection<KeyValueObject<string, string>>();

            ParameterTypeList.Add(new KeyValueObject<string, string>() { Key = "001", Value = "Int32" });

            ParameterTypeList.Add(new KeyValueObject<string, string>() { Key = "002", Value = "Double" });

            ParameterTypeList.Add(new KeyValueObject<string, string>() { Key = "003", Value = "String" });

            ParameterTypeList.Add(new KeyValueObject<string, string>() { Key = "004", Value = "Byte[]" });

            ParameterTypeList.Add(new KeyValueObject<string, string>() { Key = "005", Value = "Boolean" });
        }



        public static ExtendProperty ParametersProperty = RegisterProperty(v => v.Parameters);
        /// <summary>
        /// 参数集合
        /// </summary>
        public SerializableObservableCollection<ParameterInfo> Parameters { set { SetValue(ParametersProperty, value); } get { return (SerializableObservableCollection<ParameterInfo>)GetValue(ParametersProperty); } }

        /// <summary>
        /// 参数类型集合
        /// </summary>
        public static ObservableCollection<KeyValueObject<string, string>> ParameterTypeList { set; get; }

        /// <summary>
        /// 设置参数列表
        /// </summary>
        /// <param name="parameters"></param>
        public void SetParameters(SerializableObservableCollection<ParameterInfo> parameters)
        {
            this.loadCount = 0;
            this.Parameters = parameters;
            this.loadCount = 0;
        }


        public void InitWithParameters()
        {
            this.loadCount = 0;
            foreach (var p in Parameters)
            {
                try
                {
                    p.ParameterType = ParameterTypeList.First(ptl => ptl.Key == p.ParameterType.Key);
                }
                catch { }
            }
        }
    }
}
